package com.android.hidephotosplus;

import java.util.ArrayList;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.MenuItem;
import com.android.hidephotosplus.adapter.DrawerAdapter;
import com.android.hidephotosplus.db.DbHelper;
import com.android.hidephotosplus.db.DbProvider;
import com.android.hidephotosplus.interfaces.MInterface.onTask;
import com.android.hidephotosplus.model.ModelDrawer;
import com.android.hidephotosplus.model.ModelMedia;
import com.android.hidephotosplus.util.StorageHelper;
import com.android.hidephotosplus.util.Util;

public class MainActivity extends SherlockFragmentActivity {

	public static DrawerLayout mDrawerLayout;

	private ActionBarDrawerToggle mDrawerToggle;

	private ListView mDrawerList;

	private DbProvider mDbProvider;

	private ActionBar mActionBar;

	private DrawerAdapter mDrawerAdapter;

	private ArrayList<ModelDrawer> arrDrawers;

	private DbHelper mDbHelper;

	private StorageHelper mStorageHelper;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		mDbProvider = DbProvider.getinstance(getApplicationContext());
		mDbHelper = DbHelper.getInstance(getApplicationContext());
		mStorageHelper = new StorageHelper();

		// Check storage
		if (mStorageHelper.isExternalStorageAvailableAndWriteable()) {
			// result action send
			resultActionSend();

			// actionbar
			mActionBar = getSupportActionBar();
			mActionBar.setDisplayShowTitleEnabled(false);
			mActionBar.setHomeButtonEnabled(true);
			mActionBar.setIcon(R.drawable.ic_drawer_home);
			mActionBar.setBackgroundDrawable(getResources().getDrawable(
					R.drawable.bg_actionbar));

			// ----------------- List drawer
			mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
			mDrawerList = (ListView) findViewById(R.id.listViewDrawer);
			arrDrawers = getDrawerItem();
			mDrawerAdapter = new DrawerAdapter(getApplicationContext(),
					arrDrawers);
			mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow,
					GravityCompat.START);
			mDrawerList.setAdapter(mDrawerAdapter);
			mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
			mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
					R.drawable.ic_drawer, R.string.drawer_open,
					R.string.drawer_close) {
				public void onDrawerClosed(View view) {

				}

				public void onDrawerOpened(View drawerView) {
				}
			};
			mDrawerLayout.setDrawerListener(mDrawerToggle);
			getSupportFragmentManager().beginTransaction()
					.replace(R.id.content_frame, new FragmentMain()).commit();
		} else {
			Toast.makeText(getApplicationContext(), "no Sdcard",
					Toast.LENGTH_SHORT).show();
		}

	}

	private class DrawerItemClickListener implements
			ListView.OnItemClickListener {
		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {

			if (arrDrawers.get(position).getName()
					.equals(getString(R.string.security_settings))) {
				startActivity(new Intent(MainActivity.this,
						SecurityPreference.class));
			} else if (arrDrawers.get(position).getName()
					.equals(getString(R.string.app_settings))) {
				startActivity(new Intent(MainActivity.this, AppSettings.class));
			}

		}
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			if (mDrawerLayout.isDrawerOpen(mDrawerList)) {
				mDrawerLayout.closeDrawer(mDrawerList);
			} else {
				mDrawerLayout.openDrawer(mDrawerList);
			}
			break;

		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	private void resultActionSend() {
		// ===========================================
		Intent intent = getIntent();
		if (Intent.ACTION_SEND.equals(intent.getAction())) {
			if (intent.getExtras() != null) {
				finish();
				Uri pathMedia = (Uri) intent
						.getParcelableExtra(Intent.EXTRA_STREAM);
				ModelMedia media = mDbProvider.getMediaByUri(pathMedia);
				if (media != null) {
					ArrayList<ModelMedia> arrMedias = new ArrayList<ModelMedia>();
					arrMedias.add(media);
					new Util.HiddenMedia(this, arrMedias, null, null, false,
							new onTask() {
								@Override
								public void onTaskComplete(Object object) {
									Toast.makeText(
											getApplicationContext(),
											getString(R.string.media_was_hidden),
											Toast.LENGTH_SHORT).show();
								}
							}).execute();
				} else
					Toast.makeText(getApplicationContext(),
							getString(R.string.hide_media_failed),
							Toast.LENGTH_SHORT).show();
			}
		} else if (Intent.ACTION_SEND_MULTIPLE.equals(intent.getAction())) {
			finish();
			ArrayList<Uri> Uris = intent
					.getParcelableArrayListExtra(Intent.EXTRA_STREAM);
			final ArrayList<ModelMedia> arrMedias = new ArrayList<ModelMedia>();
			if (Uris != null && Uris.size() > 0) {
				for (Uri uri : Uris) {
					ModelMedia media = mDbProvider.getMediaByUri(uri);
					if (media != null) {
						arrMedias.add(media);
					}
				}
				if (arrMedias.size() > 0) {
					new Util.HiddenMedia(this, arrMedias, null, null, false,
							new onTask() {
								@Override
								public void onTaskComplete(Object object) {
									Toast.makeText(
											getApplicationContext(),
											arrMedias.size()
													+ " "
													+ getString(R.string.medias_were_hidden),
											Toast.LENGTH_SHORT).show();
								}
							}).execute();
				} else
					Toast.makeText(getApplicationContext(),
							getString(R.string.hide_media_failed),
							Toast.LENGTH_SHORT).show();

			}
		}
	}

	private ArrayList<ModelDrawer> getDrawerItem() {
		ArrayList<ModelDrawer> arrDrawers = new ArrayList<ModelDrawer>();
		String[] listDrawer = getResources().getStringArray(R.array.listDrawer);
		int[] resources = { R.drawable.ic_setting, R.drawable.ic_setting,
				R.drawable.ic_setting, R.drawable.ic_setting };
		for (int i = 0; i < listDrawer.length; i++) {
			ModelDrawer item = new ModelDrawer();
			item.setName(listDrawer[i]);
			item.setResource(resources[i]);
			arrDrawers.add(item);
		}
		return arrDrawers;

	}

	// @Override
	// protected void onActivityResult(int requestCode, int resultCode, Intent
	// data) {
	// super.onActivityResult(requestCode, resultCode, data);
	// Log.e("resultCode", "---");
	// switch (requestCode) {
	// case Config.REQUEST_CODE_SETUP_PATTERN_PASSWORD:
	// if (resultCode == RESULT_OK) {
	// char[] pattern = data
	// .getCharArrayExtra(LockPatternActivity.EXTRA_PATTERN);
	// String valuePattern = new String(pattern);
	// mDbHelper.insertValue(DbHelper.KEY_PATTERN_PASSWORD,
	// valuePattern);
	// IS_START_PASSWORD = false;
	// }
	// break;
	// case Config.REQUEST_CODE_START_PATTERN_PASSWORD:
	// if (resultCode == RESULT_OK) {
	// IS_START_PASSWORD = false;
	// }
	// break;
	// default:
	// break;
	// }
	//
	// }

}
